package com.bawei.demo_mvvm.viewmodel

import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.bawei.demo_mvvm.base.BaseViewModel
import com.bawei.demo_mvvm.entity.VideoEntity
import com.bawei.demo_mvvm.repository.VideoRepository
import com.bawei.demo_mvvm.state.VideoState
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asSharedFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.catch
import kotlinx.coroutines.launch
import javax.inject.Inject

/**
 * @author yaotianxue
 * @description
 * @date 2025/5/12
 */
@HiltViewModel
class VideoViewModel @Inject constructor(private val videoRepository: VideoRepository):BaseViewModel(){
    //方案一：liveData
    private val _videoLiveData by lazy { //私有可变
        MutableLiveData<List<VideoEntity>>()
    }
    val videoLiveData:LiveData<List<VideoEntity>> = _videoLiveData //公开不可变,只能订阅
    //方案二：ShareFlow共享流
    private val _videoShareFlow by lazy {//私有可变
        MutableSharedFlow<List<VideoEntity>>()
    }
    val videoShareFlow = _videoShareFlow.asSharedFlow() //公开不可变,只能订阅,调用asSharedFlow


    //方案三：StateFlow状态流
    private val _videoStateFlow by lazy {//私有可变
        MutableStateFlow<VideoState>(VideoState.Loading) //必须有初始值，必须有状态
    }
    val videoStateFlow = _videoStateFlow.asStateFlow()




    //仓库
//    private val videoRepository:VideoRepository by lazy {
//        VideoRepository()
//    }
    //方案一
    fun video(){
        //协程作用域：flow必须在协程作用域中运行
//        viewModelScope.launch {
//            val video = videoRepository.video(1)
//            video.collect{//收集数据，更新liveData
//                _videoLiveData.value = it.data
//            }
//        }
        execute(
            action = videoRepository.video(1),
            onFinish = {
                _videoLiveData.value = it.data
            }
        )
    }

    //方案二
    fun video2(){
        //协程作用域：flow必须在协程作用域中运行
//        viewModelScope.launch {
//            val video = videoRepository.video(1)
//            video.collect{//收集数据，更新stateFlow
//                _videoShareFlow.emit(it.data)
//            }
//        }
        execute(
            action = videoRepository.video(1),
            onFinish = {
                _videoShareFlow.emit(it.data)
            }
        )
    }

    //方案三
    fun video3(){
        //协程作用域：flow必须在协程作用域中运行
        execute(
            action = videoRepository.video(1),
            onStart = {
                _videoStateFlow.value = VideoState.Loading //数据加载中
            } ,
            onFinish = {
                _videoStateFlow.value = VideoState.Success(it.data) //成功
            } ,
            onError = {
                _videoStateFlow.value = VideoState.Error(it) //失败
            }
        )
//        viewModelScope.launch {
//            _videoStateFlow.value = VideoState.Loading //数据加载中
//            val video = videoRepository.video(1)
//            video.catch {
//                _videoStateFlow.value = VideoState.Error(it.message.toString()) //失败
//            }.collect{
//                _videoStateFlow.value = VideoState.Success(it.data) //成功
//            }
//        }
    }

}